package com.itheima.reggie.controller;

import com.itheima.reggie.common.OssTemplate;
import com.itheima.reggie.common.ResultInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * 文件图片上传
 * @author zsk
 * @create 2022-02-15-17:50
 */
@RestController
public class FileController {

    //依赖注入阿里云文件上传工具类
    @Autowired
    private OssTemplate ossTemplate;

    /*
    图片上传
    Request URL: http://localhost:8080/common/upload
    Request Method: POST
     */
    @PostMapping("/common/upload")
    public ResultInfo uploadFile(MultipartFile file) throws IOException {
        //判断
        if (file.getSize() > 0){
            //将接受到的文件上传到阿里云服务器
            String filePath = ossTemplate.upload(file.getOriginalFilename(), file.getInputStream());

            //返回给前端结果
            return ResultInfo.success(filePath);
        }
        return ResultInfo.error("文件上传失败");
    }
}
